<?php
/*
 *      oslogin.php
 *      
 *      Copyright 2011 ViRak <vetalrakitin@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/oscomponent.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/osdbcombobox.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/osbutton.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/osedit.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/osmsgbox.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interface/osadminka.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/DB/osdbconnect.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/interfaces/seller.php");
require_once($_SERVER['DOCUMENT_ROOT']."/classes/osuser.php");

//class oslogin extends oscomponent {
class oslogin extends oswindow {
	
	private $cboffices;
	private $cbusers;
	private $edit1;
	//private $button1;
	//private $username;
	//private $userpassword;
	//private $btadminka;
	private $dbconnect;
	private $btlogin;
	//private $btreset;
	
	public $caption = "Окно входа";
	public $height = 130;
	public $width = 380;
	public $aligment = "center";
	
	function __construct($parent) {
		parent::__construct($parent);

		$this->dbconnect = new osdbconnect($this->uuid);
		$this->dbconnect->dbname = "helper";
		$this->dbconnect->host = "localhost";
		$this->dbconnect->username = "loginuser";
		$this->dbconnect->password = "loginpassword";
		if (!$this->dbconnect->connect()) {
			new osmsgbox("Невозможно подключится к базе данных");
		} else {
/*
			$list = $this->dbconnect->get_table("interfaces");
			foreach($list as $item) {
				require_once($_SERVER['DOCUMENT_ROOT'].$item["path"]);
			}
*/
		}
		
		$this->cboffices = new oscombobox($this->uuid);
		$this->cboffices->setevent($this->uuid, "onchange", "cbofficesonchange");
		if ($this->dbconnect->active) {
			foreach ($this->dbconnect->get_table("offices") as $office) {
				$this->cboffices->additem($office["code"], $office["name"]);
				
			}
		}
		
		$this->cbusers = new oscombobox($this->uuid);
		foreach ($this->dbconnect->get_table("users",array("office" => $this->cboffices->selected)) as $user) {
				$this->cbusers->additem($user["code"], $user["name"]);
				$this->cbusers->setproperty($user["code"], array("dbusername" =>$user["dbusername"]));
			}
		
		$this->edit1 = new osedit($this->uuid);
		$this->edit1->setevent($this->uuid, "onkeypress", "edit1onkeypress");
		
		$this->btlogin = new osbutton($this->uuid);
		$this->btlogin->setevent($this->uuid, "onclick", "btloginonclick");
		$this->btlogin->caption = "login";
		
/*
		$this->btreset = new osbutton($this->uuid);
		$this->btreset->setevent($this->uuid, "onclick", "btresetonclick");
		$this->btreset->caption = "reset";
*/
		//$this->button1->accesskey = "+";
		
/*
		$this->btadminka = new osbutton($this->uuid);
		$this->btadminka->setevent($this, "onclick", "btadminkaonclick");
		$this->btadminka->caption = "Админка";
		$this->btadminka->accesskey = "a";
		
*/
		$this->append();
		$this->edit1->setfocus();
	}
	
	function onclose() {
		return false;
	}
	
	function btadminkaonclick() {
		$this->enable();
		$this->update();
	}
	
	function btloginonclick() {
		$_SESSION["components"]["CurrentUser"] = new osuser(0);
		$_SESSION["components"]["CurrentUser"]->dbconnect->username = $this->cbusers->getproperty($this->cbusers->selected, "dbusername");
		$_SESSION["components"]["CurrentUser"]->dbconnect->password = $this->edit1->value;
		$_SESSION["components"]["CurrentUser"]->dbconnect->dbname = "helper";
		$_SESSION["components"]["CurrentUser"]->dbconnect->host = "localhost";
		if (!$_SESSION["components"]["CurrentUser"]->dbconnect->connect()) {
			new osmsgbox("Неверно указан логин/пароль");
			return 0;
		}
		
		$list = $this->dbconnect->get_table("offices",array("code" => $this->cboffices->selected));
		//new osmsgbox("<pre>".print_r($list,1)."</pre>");
		if (array_key_exists(0,$list)) {
			$interface_code = $list[0]["interface"];
			$list = $this->dbconnect->get_table("interfaces",array("code" => $interface_code));
			if (array_key_exists(0,$list)) {
				//if (file_exists($_SERVER['DOCUMENT_ROOT'].$list[0]["path"])) {
					//require_once($_SERVER['DOCUMENT_ROOT'].$list[0]["path"]);
					if (class_exists($list[0]["class"])) {	
						//$this->hide();					
						
						$_SESSION["components"]["CurrentInterface"] = new $list[0]["class"](0);
						$this->destroy();
					} else {
						new osmsgbox("Класс интерфейса не найден");
					}
				//} else {
				//	new osmsgbox("Файл интерфейса не найден");
				//}
			} else {
				new osmsgbox("Интерфейс должности не определен");
			}
		}
	}
	
/*
	function edit1onchange($value) {

	}
*/
	
	function cbofficesonchange($value) {
		if ($this->dbconnect->active) {
			$this->cbusers->deleteallitems();
			foreach ($this->dbconnect->get_table("users",array("office" => $value)) as $user) {
				$this->cbusers->additem($user["code"], $user["name"]);
				$this->cbusers->setproperty($user["code"], array("dbusername" =>$user["dbusername"]));
			}
			$this->cbusers->update();
		}
	}
	
	function edit1onkeypress($key) {
		if ($key == 13) {
			$this->btloginonclick();
		}
	}
	
	function get() {
		//$ret = "<div id=".$this->uuid.">";
		//if (!$this->active) {
			// Вывести экран входа
			$ret = "<table align=center><tr>";
			$ret .= "<td>Должность пользователя:</td>";
			$ret .= "<td>".$this->cboffices->get()."</td>";
			$ret .= "</tr><tr>";
			$ret .= "<td>Имя пользователя:</td>";
			$ret .= "<td>".$this->cbusers->get()."</td>";
			$ret .= "</tr><tr>";
			$ret .= "<td>Пароль пользователя:</td>";
			$ret .= "<td>".$this->edit1->get()."</td>";
			$ret .= "</tr></table>";
			//$ret .= "<pre>".print_r($this->edit1,1)."</pre>";
			$ret .= "<div align=center>";
			$ret .= $this->btlogin->get();
			//$ret .= $this->btreset->get();
			$ret .= "</div>";
			//$ret .= $this->btadminka->get();
		//} else {
			// Вывести интерфейс пользователя
			//$dic = $_SESSION["data"]->dictionaries->Пользователи;
			//$dic->searchbyfield();
		//	$adminka = new osadminka(0);
		//	$ret .= $adminka->get();//"Интерфейс пользователя ".$this->username;
		//}
		//$ret .= "</div>";
		return parent::get($ret);
	}
}
?>
